package top.rainbowecho.gateway.security.jwt;

import org.bouncycastle.crypto.tls.UserMappingType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import top.rainbowecho.gateway.mapper.UsersMapper;
import top.rainbowecho.gateway.security.property.SecurityProperties;

import javax.annotation.Resource;

/**
 * @author rainbow
 * @since 2019/12/27 16:41
 */
@Configuration
public class JwtTokenConfig {
    private SecurityProperties securityProperties;

    private UsersMapper usersMapper;

    @Resource
    public void setUsersMapper(UsersMapper usersMapper) {
        this.usersMapper = usersMapper;
    }

    @Autowired
    public void setSecurityProperties(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        tokenConverter.setSigningKey(this.securityProperties.getJwt().getSignKey());
        return tokenConverter;
    }

    @Bean
    public TokenStore jwtTokenStore(JwtAccessTokenConverter tokenConverter) {
        return new JwtTokenStore(tokenConverter);
    }

    @Bean
    public TokenEnhancer tokenEnhancer() {
        JWTokenEnhancer tokenEnhancer = new JWTokenEnhancer();
        tokenEnhancer.setUsersMapper(this.usersMapper);
        return tokenEnhancer;
    }
}
